package com.nf.action;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.StrutsStatics;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.nf.entity.King;

import java.io.PrintWriter;


public class TestAction extends ActionSupport {
    @Override
    public String execute() throws Exception {
        ActionContext ac = ActionContext.getContext();
        //Object response = ac.get(StrutsStatics.HTTP_RESPONSE);
        HttpServletResponse response =(HttpServletResponse) ac.get(StrutsStatics.HTTP_RESPONSE);
        HttpServletRequest request = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST);

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //response.setContentType("text/html;charset=utf-8");
        response.setContentType("application/json;charset=utf-8");


        King king1 = new King();
        king1.setLevel(100);
        king1.setHero("牛魔");

        //System.out.println(response);
        Gson gson = new GsonBuilder().create();
        String aaa = gson.toJson(king1);
        System.out.println(aaa);

        PrintWriter out = response.getWriter();
        out.print(aaa);
        return this.NONE;
    }
}
